from django.db import models

# Create your models here.

from users.models import User


class BaseModel(models.Model):
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        abstract = True


class Troubles(BaseModel):

    title = models.CharField(max_length=16, verbose_name='问题标题')
    content = models.CharField(max_length=1024, verbose_name='问题内容')
    user = models.ForeignKey(User, on_delete=models.PROTECT, related_name='user_troubles', verbose_name='提问的用户')
    answer_count = models.IntegerField(default=0,verbose_name='回答数')
    new_answer_name = models.CharField(max_length=16, verbose_name='最新回答名字', default='', null=True, blank=True)
    new_answer_time = models.DateTimeField(max_length=16, verbose_name='最新回答时间', default='', null=True, blank=True)  #每次回答问题往数据库添加一个时间
    like_count = models.IntegerField(default=0, verbose_name='有用数')
    look_count = models.IntegerField(default=0, verbose_name='浏览量')
    is_over = models.BooleanField(default=False, verbose_name='是否解决')
    is_delete = models.BooleanField(default=False, verbose_name='是否删除')


    class Meta:
        db_table = 'tb_troubles'
        verbose_name = '提问'
        verbose_name_plural = verbose_name


class Respond(BaseModel):


    content = models.CharField(max_length=512,verbose_name='回答内容')
    user = models.ForeignKey(User, on_delete=models.PROTECT, related_name='user_respond', verbose_name='回答的用户')
    question = models.ForeignKey(Troubles, on_delete=models.CASCADE, verbose_name='提问的回答')
    comment_count = models.IntegerField(default=0, verbose_name='评论数')
    is_delete = models.BooleanField(default=False, verbose_name='是否删除')

    class Meta:
        db_table = 'tb_respond'
        verbose_name = '回答'
        verbose_name_plural = verbose_name


class RespondComment(BaseModel):

    content = models.CharField(max_length=256, verbose_name='评论内容')
    user = models.ForeignKey(User, on_delete=models.PROTECT, related_name='user_comment', verbose_name='评论用户')
    respond = models.ForeignKey(Respond, on_delete=models.CASCADE, verbose_name='回答的评论')
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='subs', null=True, blank=True,
                               verbose_name='上级评论')

    class Meta:
        db_table = 'tb_respond_comment'
        verbose_name = '评论'
        verbose_name_plural = verbose_name


class NavigationLabel(models.Model):

    name = models.CharField(max_length=16, verbose_name='导航标签名字')
    explanation = models.CharField(max_length=256, verbose_name='导航标签说明')
    fans = models.IntegerField(default=0,verbose_name='关注人数')
    ico = models.CharField(max_length=200, default='', null=True, blank=True, verbose_name='标签图标')

    class Meta:
        db_table = 'tb_navigation_label'
        verbose_name = '导航标签'
        verbose_name_plural = verbose_name


class Label(models.Model):


    name = models.CharField(max_length=16, verbose_name='标签名字')

    class Meta:
        db_table = 'tb__label'
        verbose_name = '标签'
        verbose_name_plural = verbose_name

class UserNavigationLabel(models.Model):


    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='用户')
    navigation_label = models.ForeignKey(NavigationLabel, on_delete=models.PROTECT, verbose_name='标签')

    class Meta:
        db_table = 'tb_user_navigation_label'
        verbose_name = '导航标签与用户关系表'
        verbose_name_plural = verbose_name

class NavigationLabelSubs(models.Model):

    navigation_label = models.ForeignKey(NavigationLabel, on_delete=models.PROTECT, verbose_name='导航标签')
    label = models.ForeignKey(Label, on_delete=models.PROTECT, verbose_name='标签')

    class Meta:
        db_table = 'tb_navigation_label_subs'
        verbose_name = '导航标签与标签表'
        verbose_name_plural = verbose_name


class TroublesLabel(models.Model):


    navigation_label = models.ForeignKey(NavigationLabel, on_delete=models.PROTECT, verbose_name='导航标签')

    troubles = models.ForeignKey(Troubles, on_delete=models.PROTECT, verbose_name='提问')

    class Meta:
        db_table = 'tb_troubles_label'
        verbose_name = '问题与标签表'
        verbose_name_plural = verbose_name


class TroublesLike(BaseModel):
    GENDER_CHOICES = (

        (1, '顶'),
        (2, '踩')
    )
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='用户')
    like_state = models.SmallIntegerField(default=0, choices=GENDER_CHOICES, verbose_name='顶踩状态')
    question = models.ForeignKey(Troubles, on_delete=models.CASCADE, verbose_name='提问的顶踩')
    is_delete = models.BooleanField(default=0, verbose_name='是否删除')
    class Meta:
        db_table = 'tb_troubles_like'
        verbose_name = '问题的顶踩'
        verbose_name_plural = verbose_name